 using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using MedicService.Domain;
using MedicService.Domain.Generic;
using MedicService.Domain.Print;
using MedicService.Server.Factory.Generic;
using MedicService.Server.Factory.Print;

namespace MedicService.Server.Factory {
    public class OrderFactory : EntityFactory<Order> {

        public OrderFactory(int year) : base("Order", " date DESC", " date >= " + Date(new DateTime(year, 1, 1)) + " AND date < " + Date(new DateTime(year+1, 1, 1))) { }

        public OrderFactory() : base("Order", " date DESC") { }

        protected override Order CreateEntity() {
            return new Order();
        }

        protected override string GetInsertQuery(Order entity) {
            string q = "INSERT INTO [Order]" +
                       "([date]" +
                       ",[number]" +
                       ",[content]" +
                       ",[isclosed]" +
                       ",[ProtocolDate]" +
                       ",[ProtocolNumber])" +
                       "VALUES" +
                       "(" + Date(entity.Date) + "" +
                       " ," + Nullable(entity.Number) +
                       " ," + Nullable(entity.Content) +
                       " ," + Nullable(entity.IsClosed) +
                       " ," + Date(entity.ProtocolDate) +
                       " ," + Nullable(entity.ProtocolNumber) + ");" +
                       " ";
            return q;
        }

        protected override string GetUpdateQuery(Order entity) {
            string q = "UPDATE [Order] " +
                       "SET [date] = " + Date(entity.Date) +
                       " ,[number] = " + Nullable(entity.Number) +
                       " ,[content] = " + Nullable(entity.Content) +
                       " ,[isclosed] = " + entity.IsClosed +
                       " ,[ProtocolDate] = " + Date(entity.ProtocolDate) +
                       " ,[ProtocolNumber] = " + Nullable(entity.ProtocolNumber) +
                       " WHERE id = " + entity.Id;
            return q;
        }

        protected override void FillEntity(Order entity, DataRow myDataRow) {
            base.FillEntity(entity, myDataRow);
            entity.Content = myDataRow["content"] as string;
            entity.Date = (DateTime) myDataRow["date"];
            entity.IsClosed = int.Parse("" + myDataRow["isclosed"]);
            entity.Number = myDataRow["number"] as string;
            if (Convert.IsDBNull(myDataRow["ProtocolDate"])) {
                entity.ProtocolDate = null;
            } else {
                entity.ProtocolDate = (DateTime)myDataRow["ProtocolDate"];
            }
            entity.ProtocolNumber = myDataRow["ProtocolNumber"] as string;
        }


        //private static string Date(DateTime dateTime) {
        //    return "'" + dateTime.Year + "-" + dateTime.Month + "-" + dateTime.Day + "'";
        //}


        //private static object Nullable(Entity item) {
        //    if (item == null || item.Id == 0) {
        //        return "NULL";
        //    } else {
        //        return "'" + item.Id + "'";
        //    }
        //}

        //private static string Nullable(object o) {
        //    if (o == null) {
        //        return "NULL";
        //    } else {
        //        return "'" + o + "'";
        //    }
        //}
    }
}